// pages/storesIntrod/storesIntrod.js
var api = require('../../utils/api.js');
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    company:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //获取门店详情
    try {
      var company = wx.getStorageSync('company')
      console.log(company)
      if (company) {
        WxParse.wxParse('desc', 'html', company.desc, this, 30);
        that.setData({
          company: company
        });
      } else {
        that.getCompany();
      }
    } catch (e) {
      that.getCompany();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCompany();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 获取公司资料
   */
  getCompany:function(){
    console.log('company')
    
    var that = this;
    api.get({
      url: 'portal/index/getCompanyInfo',
      success: data => {
        console.log(data)
        if (data.code) {
          var company = data.data

          WxParse.wxParse('desc', 'html', company.desc, this, 30);

          that.setData({
            company: company
          });

          //加入缓存
          wx.setStorage({
            key: "company",
            data: company
          })


        }else{
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      }
    });
  },
  /**
   * 点击显示地图
   */
  showMap: function () {
    var that = this;
    //获取经纬度
    var lng = Number(that.data.company.lng);
    var lat = Number(that.data.company.lat);

    wx.openLocation({
      latitude: lat,
      longitude: lng,
      scale: 28
    })
  },
  //打电话
  callEvent: function () {
    var callTel = this.data.company.mobile;
    console.log(callTel)

    wx.makePhoneCall({
      phoneNumber: callTel
    })
  },
  /**
   * call销售
   */
  callSeller:function(e){
    console.log(e);
    var callTel = e.currentTarget.dataset.tel;
 
    wx.makePhoneCall({
      phoneNumber: callTel
    })
  }
})